<%--
  Created by IntelliJ IDEA.
  User: brss
  Date: 2017/8/5
  Time: 10:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>分类管理</title>
</head>
<body>

<div class="panel-group">
    <div class="panel panel-primary">
        <div class="panel-heading">
            分类管理
        </div>
        <div class="panel-body">
            <div class="list-op" id="list_op">
                <button type="button" class="btn btn-default btn-sm" id="addCategory">
                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
                </button>
                <button type="button" class="btn btn-default btn-sm" id="modifyCategory">
                    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
                </button>
                <button type="button" class="btn btn-default btn-sm" id="deleteCategory">
                    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
                </button>
            </div>
        </div>
        <table class="table table-bordered table-hover" id="table">
        </table>
        <div class="panel-footer"><!-- panel的foot -->
            <%--这是panel的footer--%>
        </div><!-- end of panel-footer -->
    </div><!-- end of panel -->
</div>

<script>
    $(function () {
    });//加载完成后事件over

    $('#table').bootstrapTable({
        pageSize: 10,
        pagination: true,//设置为 true 会在表格底部显示分页条
        sidePagination: 'server',//设置在哪里进行分页，可选值为 'client' 或者 'server'。设置 'server'时，必须设置 服务器数据地址（url）或者重写ajax方法
        clickToSelect: true,
        url: '/category/list',
        columns: [{
            checkbox: true
        },{
            field: 'categoryId',
            title: 'ID编号'
        }, {
            field: 'categoryName',
            title: '分类名称'
        }, {
            field: 'categoryDescription',
            title: '分类说明'
        }, ]
    });
    //跳转到新增分类的页面
    $("#addCategory").click(function (e) {
        $("#main-content").load("/admin/adminCategoryForm");
    });
    //跳转到修改分类的页面
    $("#modifyCategory").click(function (e) {
        //获取到你选择了哪些行（数组 ， 里面装的对象）
        var arr = $('#table').bootstrapTable('getSelections');
        if(arr.length>1){
            alert("只能选择一项进行修改");
            return;
        }else if(arr.length==0){
            alert("请选择一项");
            return;
        }
        //console.log(arr[0]['categoryId'])
        $("#main-content").load("/admin/adminCategoryForm",function(responseTxt,statusTxt,xhr){
            $("#categoryId").val(arr[0]['categoryId']);
            $("#categoryName").val(arr[0]['categoryName']);
            $("#categoryDescription").val(arr[0]['categoryDescription']);
        });
    });
    //删除分类
    $("#deleteCategory").click(function (e) {
        var arr = $('#table').bootstrapTable('getSelections');
        if(arr.length==0){
            alert("请至少选择一项");
            return;
        }
        var str = "";
        for(var i=0 ; i < arr.length ; i++){
            str+=arr[i]['categoryId'] + ",";
        }
        //删除末尾多余的那个逗号
        str = str.substring(0,str.length-1);
        $.get("/category/delete", { ids: str },function (data) {
            data = $.parseJSON(data);
            alert(data.msg);
            $("#main-content").load("/admin/adminManageCategory");
        } );
    });
</script>

</body>
</html>
